package com.atjava.nebula.weixin;

import java.util.Map;

import com.atjava.nebula.core.exception.baseExp.BusinessException;

public class WeixinSendContextMake {

	public static final String IMAGE_TYPE = "image";
	public static final String VIDEO_TYPE = "video";
	public static final String VOICE_TYPE = "voice";
	public static final String FILE_TYPE = "file";
	public static final String MPNEWS_TYPE = "mpnews";
	public static final String NEWS_TYPE = "news";
	public static final String TEXT_TYPE = "text";
	
	private String userList = null;
	private String partyList = null;
	private String togList = null;
	private String agentid = null;
	private String content = null;
	private String saveType = null;
	private String type = null;
	private String media_id = null;
	private String title = null;
	private String description = null;
	private String url = null;
	private String picurl = null;
	private String thumb_media_id = null;
	private String author = null;
	private String content_source_url = null;
	private String digest = null;
	private String show_cover_pic = null;
	
	public WeixinSendContextMake(Map<String,String> HashMap,String infoType){
		this.type = infoType;
		this.userList = HashMap.get("touser");
		this.partyList = HashMap.get("toparty");
		this.togList = HashMap.get("totag");
		this.agentid = HashMap.get("agentid"); 
		this.content = HashMap.get("content");
		this.saveType = HashMap.get("safe");
		this.media_id = HashMap.get("media_id");
		this.title = HashMap.get("titile");
		this.description = HashMap.get("description");
		this.url = HashMap.get("url");
		this.picurl = HashMap.get("picurl");
		this.thumb_media_id = HashMap.get("thumb_media_id");
		this.author = HashMap.get("author");
		this.content_source_url = HashMap.get("content_source_url");
		this.digest = HashMap.get("digest");
		this.show_cover_pic = HashMap.get("show_cover_pic");
		
	}
	public String makeSendToWeixinJsonString() throws BusinessException{
		
		if(this.agentid == null || this.agentid.equals("")){
			throw new BusinessException("微信参数(agentid)错误");
		}
		if(this.type.equals(TEXT_TYPE)){
			return makeTextContext();
		}else if(this.type.equals(IMAGE_TYPE)){
			return makeMediaFileContext();
		}else if(this.type.equals(VIDEO_TYPE)){
			return makeMediaFileContext();
		}else if(this.type.equals(VOICE_TYPE)){
			return makeMediaFileContext();
		}else if(this.type.equals(FILE_TYPE)){
			return makeMediaFileContext();
		}else if(this.type.equals(MPNEWS_TYPE)){
			return makeMpNewsContext();
		}else if(this.type.equals(NEWS_TYPE)){
			return makeNewsContext();
		}
		
			
		return null;
	}
	
	private String getBaseInfoMake(){
		
		StringBuilder sb = new StringBuilder();
		
		if(this.userList != null && !this.userList.equals("") )
			sb.append("\"touser\": \""+this.userList+"\",");
			
		if(this.partyList != null && !this.partyList.equals("") )
			sb.append("\"toparty\": \""+this.partyList+"\",");
			
		if(this.togList != null && !this.togList.equals("") )
			sb.append("\"totag\": \""+this.togList+"\",");
			
		return sb.toString();
	}
	
	private  String makeTextContext() throws BusinessException{
		
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		
		sb.append(getBaseInfoMake());
		
		sb.append("\"msgtype\": \"text\",");
		sb.append("\"agentid\": \""+this.agentid+"\",");
		sb.append("\"text\": {\"content\": \""+this.content+"\"}");
		
		if(this.saveType != null && !this.saveType.equals("") ){
			sb.append("，\"safe\": \""+this.saveType+"\"");
		}
		sb.append("}");
		return sb.toString();
	}
	
	private  String makeMediaFileContext() throws BusinessException{
		
		if(this.media_id == null || this.media_id.equals("")){
			throw new BusinessException("微信参数(media_id)错误");
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append(getBaseInfoMake());
		sb.append("\"msgtype\": \""+this.type+"\",");
		sb.append("\"agentid\": \""+this.agentid+"\",");
		if(this.type.equals(VIDEO_TYPE)){
			sb.append("\""+this.type+"\": {\"media_id\": \""+this.media_id+"\"");
			
			if(this.title != null && !this.title.equals("") )
				sb.append(",\"title\": \""+this.title+"\"");
			
			if(this.description != null && !this.description.equals("") )
				sb.append(",\"description\": \""+this.description+"\"");
			
			sb.append("}");
			
		}else{
			sb.append("\""+this.type+"\": {\"media_id\": \""+this.media_id+"\"}");
		}
		if(this.saveType != null && !this.saveType.equals("") ){
			sb.append("，\"safe\": \""+this.saveType+"\"");
		}
		sb.append("}");
		return sb.toString();
	}
	
	private String makeNewsContext(){
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		
		sb.append(getBaseInfoMake());
		
		sb.append("\"msgtype\": \"news\",");
		sb.append("\"agentid\": \""+this.agentid+"\",");
		sb.append("\"news\": {\"articles\":[{");
		
		if(this.title != null && !this.title.equals("") )
			sb.append("\"title\": \""+this.title+"\",");
		
		if(this.description != null && !this.description.equals("") )
			sb.append("\"description\": \""+this.description+"\",");
		
		if(this.url != null && !this.url.equals("") )
			sb.append("\"url\": \""+this.title+"\",");
		
		if(this.picurl != null && !this.picurl.equals("") )
			sb.append("\"picurl\": \""+this.picurl+"\",");
			
		sb.append("]}");
		sb.append("}");
		return sb.toString();

	}
	
	private String makeMpNewsContext() throws BusinessException{
		
		if(this.thumb_media_id==null || this.thumb_media_id.equals(""))
			throw new BusinessException("微信参数(thumb_media_id)错误");
		if(this.media_id==null || this.media_id.equals(""))
			throw new BusinessException("微信参数(media_id)错误");
		if(this.content==null || this.content.equals(""))
			throw new BusinessException("微信参数(content)错误");
		if(this.title==null || this.title.equals(""))
			throw new BusinessException("微信参数(title)错误");
		
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		
		sb.append(getBaseInfoMake());
		
		sb.append("\"msgtype\": \"mpnews\",");
		sb.append("\"agentid\": \""+this.agentid+"\",");
		sb.append("\"mpnews\": {\"articles\":[{");
		
		sb.append("\"thumb_media_id\": \""+this.thumb_media_id+"\",");
		sb.append("\"title\": \""+this.title+"\",");
		
		if(this.author != null && !this.author.equals("") )
			sb.append("\"author\": \""+this.author+"\",");

		if(this.content_source_url != null && !this.content_source_url.equals("") )
			sb.append("\"content_source_url\": \""+this.content_source_url+"\",");
		
		sb.append("\"content\": \""+this.content+"\",");
		
		if(this.digest != null && !this.digest.equals("") )
			sb.append("\"digest\": \""+this.digest+"\",");
		
		if(this.show_cover_pic != null && !this.show_cover_pic.equals("") )
			sb.append("\"show_cover_pic\": \""+this.show_cover_pic+"\",");
			
		sb.append("]}");
		
		if(this.saveType != null && !this.saveType.equals("") ){
			sb.append("，\"safe\": \""+this.saveType+"\"");
		}
		
		sb.append("}");
		return sb.toString();

	}
	
	
	
}
